package com.hotel.service.impl;

import com.hotel.entity.Bill;
import com.hotel.mapper.BillMapper;
import com.hotel.service.BillService;
import com.hotel.vo.BillVo;
import com.hotel.vo.ConsumptionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@Service
public class BillServiceImpl implements BillService {

    @Autowired
    private BillMapper billMapper;
//    @Autowired
//    private CheckInServiceImpl checkInService;
    @Autowired
    private ConsumptionServiceImpl consumptionService;
    @Override
    public BillVo list(Long id) {
        //根据入住id查询账单信息表
        Bill bill = billMapper.list(id);
        BillVo billVo = new BillVo();
        BeanUtils.copyProperties(bill,billVo);
        //再根据入住id查询消费记录表
        List<ConsumptionVO> consumptionVOList = consumptionService.getConsumptionByCheckInId(id);
        billVo.setConsumptionVOList(consumptionVOList);
        return billVo;
    }

    //todo 生成账单
    public void createBill(Long checkInId){
    //根据入住id查询入住信息表

//        CheckIn checkIn = checkInService.getById(checkInId);
//        BigDecimal paidAmount =checkIn.getdeposit();//已付金额
//        BigDecimal roomCharges = checkIn.getCost();//房间价格

        //根据chckInId查询消费记录表
        List<ConsumptionVO> consumptionVOList = consumptionService.getConsumptionByCheckInId(checkInId);
        //获取服务消费总额
        BigDecimal serviceTotalPrice = consumptionVOList.stream()
                .map(ConsumptionVO::getTotalPrice)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        //应支付
//        BigDecimal totalCharges = serviceTotalPrice.add(roomCharges);
        //待支付
//        BigDecimal unpaidAmount = totalCharges.subtract(paidAmount);
        //系统根据当前时间生成一个随机数
        String nonceStr = UUID.randomUUID().toString().replaceAll("-", "");
        String billNumber = "bill" + nonceStr;



        Bill bill =  Bill.builder()
                .billNumber(billNumber)
//                .roomCharges(roomCharges)
                .checkInId(checkInId)
//                .totalCharges(totalCharges)
//                .paidAmount(paidAmount)
                .serviceCharges(serviceTotalPrice)
//                .unpaidAmount(unpaidAmount)
                .build();
        billMapper.save(bill);
    }
}
